#include "BatchCheckInRequest.h"

BatchCheckInRequest::BatchCheckInRequest(const QString &factoryNo,
                                         const QString &transId,
                                         const QString &eventTime,
                                         const QString &eventUser,
                                         const QString &machineNo,
                                         const QString &batch,
                                         const QList<BatchCheckInPcuInfo> &pcuInfo)
    :factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    eventUser(eventUser),
    machineNo(machineNo),
    batch(batch),
    pcuInfo(pcuInfo)
{

}

QByteArray BatchCheckInRequest::toJson() const
{
    QJsonObject json;

    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);

    json["factoryNo"] = factoryNo;
    json["transId"] = transId.isEmpty()?transId:transIdStr;
    json["eventTime"] = eventTime;
    json["eventUser"] = eventUser;
    json["machineNo"] = machineNo;
    json["batch"] = batch;

    QJsonArray pcuInfoArray;
    for(const auto&item:pcuInfo)
    {
        QJsonObject pcuInfoObj;
        pcuInfoObj["locationNo"] = item.locationNo;
        pcuInfoObj["pcu"] = item.pcu;
        pcuInfoArray.append(pcuInfoObj);
    }
    json["pcuInfo"] = pcuInfoArray;

    return QJsonDocument(json).toJson();
}

QString BatchCheckInRequest::getEndPoint() const
{
    return "/api/getech-mes/IotController/InboundCheckAndData";
}
